<?php

namespace App\Service;

use App\Model\ConfigHouseKeeperModel;
use App\Model\ConfigPatrolModel;
use App\Model\ConfigRepairModel;
use App\Model\ExaminePatrolModel;
use App\Model\ExamineRepairModel;
use App\Model\ExamineVisitModel;
use App\Model\KeeperCommentModel;
use App\Model\OrderModel;
use App\Model\OwnerInfoModel;
use App\Model\OwnerShareInfoModel;
use App\Model\PropertyRepairModel;
use App\Model\PropertyVisitModel;
use App\Model\StaffModel;
use App\Model\StaffTypeModel;
use App\Model\TradePropertyModel;
use App\Model\UserHouseModel;
use App\Model\UserModel;
use App\Utility\DataConversion;
use App\Utility\Payment;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

class PropertyService extends BaseService
{
    /**
     * @param $uid
     * @param $house
     * @param $time
     * @param $name
     * @param $tel
     * @param $person
     * @param $is_car
     * @param $car
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function visit($uid, $house, $time, $name, $tel, $person, $is_car, $car): array
    {
        $par = [
            'uid' => $uid,
            'house' => $house,
            'time' => strtotime($time),
            'name' => $name,
            'tel' => $tel,
            'person' => $person,
            'is_car' => $is_car,
            'car' => $car
        ];

        $res = PropertyVisitModel::create()->addProperty($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        $str = $time . "有一条新的访客来访消息，请及时查看";
        $icon = "https://resourse.cnlhjt.com/upload/20230410/2597323f5d2270449b50de170ece8f28.jpg";
        $title = '访客消息';
        $type = 1;
        $nav = 6;
        $staffType = StaffTypeModel::create()->where("rule", "%," . $nav . ",%", "LIKE")->all();
        $tids = array_column($staffType, 'id');
        $staff = StaffModel::create()->where("type", $tids, "IN")->all();
        $sids = array_column($staff, 'id');
        NoticeService::getInstance()->sendNotice($sids, $title, $icon, $str, $type);

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @param $house
     * @param $time
     * @param $name
     * @param $tel
     * @param $type
     * @param $name1
     * @param $tel1
     * @param $content
     * @param $images
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function repair($uid, $house, $time, $name, $tel, $type, $name1, $tel1, $content, $images): array
    {
        $par = [
            'uid' => $uid,
            'house' => $house,
            'time' => strtotime($time),
            'name' => $name,
            'tel' => $tel,
            'type' => $type,
            'name1' => $name1,
            'tel1' => $tel1,
            'content' => $content,
            'images' => json_encode($images)
        ];

        $res = PropertyRepairModel::create()->addProperty($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        $str = $time . "有一条新的报事报修订单消息，请及时查看";
        $icon = "https://resourse.cnlhjt.com/upload/20230410/2597323f5d2270449b50de170ece8f28.jpg";
        $title = '报事报修消息';
        $type = 2;
        $nav = 2;
        $staffType = StaffTypeModel::create()->where("rule", "%," . $nav . ",%", "LIKE")->all();
        $tids = array_column($staffType, 'id');
        $staff = StaffModel::create()->where("type", $tids, "IN")->all();
        $sids = array_column($staff, 'id');
        NoticeService::getInstance()->sendNotice($sids, $title, $icon, $str, $type);

        return [
            'result' => true,
            'msg' => '提交成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getVisit($uid, $status): array
    {
        $where = ['is_deleted' => 0, 'uid' => $uid];
        if ($status != 99) {
            $where['status'] = $status;
        }

        $visits = PropertyVisitModel::create()->where($where)->order('id', 'DESC')->all();
        if (empty($visits)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($visits as $visit) {
            $data[] = [
                'id' => $visit['id'],
                'uid' => $visit['uid'],
                'house' => $visit['house'],
                'time' => date("Y-m-d H:i", $visit['time']),
                'name' => $visit['name'],
                'tel' => $visit['tel'],
                'person' => $visit['person'],
                'is_car' => $visit['is_car'],
                'car' => $visit['car'],
                'status' => $visit['status'],
                'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status'])
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $status
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getRepair($uid, $status): array
    {
        $where = ['is_deleted' => 0, 'uid' => $uid];
        if ($status != 99) {
            $where['status'] = $status;
        }
        $repairs = PropertyRepairModel::create()->where($where)->order('id', 'DESC')->all();
        if (empty($repairs)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($repairs as $repair) {
            $data[] = [
                'id' => $repair['id'],
                'uid' => $repair['uid'],
                'house' => $repair['house'],
                'time' => date("Y-m-d H:i", $repair['time']),
                'name' => $repair['name'],
                'tel' => $repair['tel'],
                'name1' => $repair['name1'],
                'tel1' => $repair['tel1'],
                'content' => $repair['content'],
                'images' => json_decode($repair['images'], true),
                'type' => ConfigService::getInstance()->getRepairType($repair['type']),
                'status' => $repair['status'],
                'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
                'code' => $repair['create_time']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getVisitDetail($uid, $id): array
    {
        $visit = PropertyVisitModel::create()->getProperty(['id' => $id, 'uid' => $uid]);
        if (!$visit) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $visit = $visit->toArray();

        $data = [
            'id' => $visit['id'],
            'uid' => $visit['uid'],
            'house' => $visit['house'],
            'time' => date("Y-m-d H:i", $visit['time']),
            'name' => $visit['name'],
            'tel' => $visit['tel'],
            'person' => $visit['person'],
            'is_car' => $visit['is_car'],
            'car' => $visit['car'],
            'status' => $visit['status'],
            'status_label' => ConfigService::getInstance()->getVisitStatus($visit['status']),
            'create_time' => date("Y-m-d H:i:s", $visit['create_time'])
        ];

        $info = ConfigService::getInstance()->getUserInfo($uid);
        $data = array_merge($data, $info);
        $examine = ExamineVisitModel::create()->getLog(['pid' => $id]);
        $data['memo'] = $examine->memo ?? '-';
        $data['examine_time'] = date("Y-m-d H:i:s", $examine->create_time ?? time());
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getRepairDetail($uid, $id): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $id, 'uid' => $uid]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '获取失败'
            ];
        }

        $rType = ConfigRepairModel::create()->getConfig(['id' => $repair->type]);

        $repair = $repair->toArray();
        $data = [
            'id' => $repair['id'],
            'uid' => $repair['uid'],
            'house' => $repair['house'],
            'time' => date("Y-m-d H:i", $repair['time']),
            'name' => $repair['name'],
            'tel' => $repair['tel'],
            'name1' => $repair['name1'],
            'tel1' => $repair['tel1'],
            'content' => $repair['content'],
            'images' => json_decode($repair['images'], true),
            'type' => ConfigService::getInstance()->getRepairType($repair['type']),
            'rType' => $rType,
            'status' => $repair['status'],
            'status_label' => ConfigService::getInstance()->getRepairStatus($repair['status']),
            'code' => $repair['create_time'],
            "create_time" => date("Y-m-d H:i:s", $repair['create_time'])
        ];

        $examine = ExamineRepairModel::create()->getLog(['pid' => $repair['id']]);
        if (!$examine) {
            $examine = [];
        } else {
            $examine = $examine->toArray();
            $info = ConfigService::getInstance()->getStaffInfo($examine['sid']);
            $data = array_merge($data, $info);
            $examine['identity_code'] = $examine['code'];
            $examine['examine_time'] = date("Y-m-d H:i:s", $examine['create_time']);
            $examine['examine_new_time'] = date("Y-m-d H:i:s", $examine['update_time']);
            $examine['offer_image'] = json_decode($examine['offer_image'], true);
            $examine['complete_image'] = json_decode($examine['complete_image'], true);
            unset($examine['update_time']);
            unset($examine['create_time']);
            unset($examine['code']);
        }
        $data = array_merge($data, ['examine' => $examine]);
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getPatrolList(): array
    {
        $start = strtotime(date("Y-m-d"));
        $end = strtotime(date("Y-m-d 23:59:59"));
        $examine = ExaminePatrolModel::create()->where("create_time", $start, '>')->where("create_time", $end, '<')->where(['is_deleted' => 0])->all();
        $eid = array_column($examine, 'code');

        $patrol = ConfigPatrolModel::create()->where(['is_deleted' => 0])->all();
        $data = [];
        foreach ($patrol as $v) {
            $par = [
                'status' => 0,
                'complete' => 0,
            ];

            $par['id'] = $v['id'];
            $par['name'] = $v['name'];
            $par['code'] = $v['code'];
            $par['image'] = $v['image'];
            $par['desc'] = $v['desc'];
            $par['address'] = $v['address'];

            foreach ($examine as $e) {
                if ($e['code'] == $v['code']) {
                    $par['complete'] = 1;
                    $par['memo'] = $e['memo'];
                    $par['status'] = $e['status'];
                    $par['author'] = $e['author'];
                    $par['images'] = json_decode($e['image'], true);
                    $par['time'] = date("Y-m-d H:i:s", $e['create_time']);
                }
            }

            $data[] = $par;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $gid
     * @param $ip
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function confirmRepair($uid, $gid, $ip): array
    {
        $repair = PropertyRepairModel::create()->getProperty(['id' => $gid]);
        if (!$repair) {
            return [
                'result' => false,
                'msg' => '订单失效'
            ];
        }
        if ($repair->uid == 0 && $repair->sid != 0) {
            $r = PropertyRepairModel::create()->update(['uid' => $uid], ['id' => $gid]);
        }

        $examine = ExamineRepairModel::create()->getLog(['pid' => $gid]);
        if (!$examine) {
            return [
                'result' => false,
                'msg' => '订单失效'
            ];
        }
        if ($examine->uid == 0) {
            $r2 = ExamineRepairModel::create()->update(['uid' => $uid], ['pid' => $gid]);
        }

        $order_no = 'u' . $uid . 's' . $examine->sid . 'p' . $examine->pid . 'c' . $examine->code;
        $name = "【报事报修】" . $order_no . '支付' . $examine->offer_price . '元';
        $orderParam = [
            'uid' => $uid,
            'rid' => $gid,
            'type' => 3,
            'order_no' => $order_no,
            'amount_real' => $examine->offer_price,
            'payment_type' => 0,
            'status' => 3,
            'order_remark' => $name
        ];

        $appid = ConfigService::getInstance()->getSys("mxappid", 'wepay');
        $mxappid = ConfigService::getInstance()->getSys("mxminiappid", 'wepay');
        $mchid = ConfigService::getInstance()->getSys("mchid", 'wepay');
        $password = ConfigService::getInstance()->getSys("mxpassword", 'wepay');
        $cert = ConfigService::getInstance()->getSys("cert", 'wepay');
        $key = ConfigService::getInstance()->getSys("key", 'wepay');
        $notifyUrl = ConfigService::getInstance()->getSys("wenotify_url", 'wepay');
        $user = UserModel::create()->getUser(['id' => $uid]);
        if (!$user || $user->we_code == '') {
            return [
                'result' => false,
                'msg' => 'openidNotFound'
            ];
        }

        $res = Payment::getInstance()->mxWepay($appid, $mxappid, $mchid, $password, $cert, $key, $user->we_code, $order_no, $examine->offer_price * 100, $name, $notifyUrl, $ip);
        $result = $res->toArray();

        $resOrder = OrderModel::create()->addOrder($orderParam);
        if (!$resOrder) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '成功',
            'data' => $result ?? 'success'
        ];
    }

    /**
     * @param $uid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getPropertyFee($uid): array
    {
        $house = UserHouseModel::create()->where(['uid' => $uid, 'status' => 1])->all();
        if (empty($house)) {
            return [
                'result' => false,
                'msg' => '暂未绑定房产'
            ];
        }
        $type = 1;
        $owner = OwnerInfoModel::create()->where(['uid' => $uid])->all();
        if (empty($owner)) {
            $owner = OwnerShareInfoModel::create()->where(['uid' => $uid])->all();
            if (empty($owner)) $type = 2;
        }

        $start = ConfigService::getInstance()->getSys('start_time', 'property_fee');
        $price = ConfigService::getInstance()->getSys('price', 'property_fee');
        $public_price = ConfigService::getInstance()->getSys('public_price', 'property_fee');
        $rubbish_price = ConfigService::getInstance()->getSys('rubbish_price', 'property_fee');
        $m = date("m", time());
        $im = date("m", strtotime($start));
        $y = date("Y", time());
        $iy = date("Y", strtotime($start));
        $data = [];
        $d = date("d", strtotime($start));
        $day = DataConversion::getInstance()->getMothDay(date('Y', strtotime($start)), date("m", strtotime($start)));
        if ($y == $iy) {
            for ($i = $im * 1; $i <= $m; $i++) {
                foreach ($owner as $value) {
                    if ($i == $im) {
                        $f = ($day - $d) / $day;
                    } else {
                        $f = 1;
                    }
                    $data[$value['id']][] = [
                        'check' => true,
                        'time' => $y . "年" . $i . '月',
                        'code' => $y . '-' . $i,
                        'price' => number_format($f * $price * $value['area_build'], 2),
                        'public_price' => number_format($f * $public_price * $value['area_build'], 2),
                        'rubbish_price' => number_format($f * $rubbish_price, 2),
                        'total_price' => number_format(number_format($f * $public_price * $value['area_build'], 2) + number_format($f * $price * $value['area_build'], 2) + number_format($f * $rubbish_price, 2), 2)
                    ];
                }

            }
        } else {
            for ($i = $im * 1; $i <= 12; $i++) {
                foreach ($owner as $value) {
                    if ($i == $im) {
                        $f = ($day - $d) / $day;
                    } else {
                        $f = 1;
                    }
                    $data[$value['id']][] = array(
                        'check' => true,
                        'time' => $y . "年" . $i . '月',
                        'code' => $y . '-' . $i,
                        'price' => number_format($f * $price * $value['area_build'], 2),
                        'public_price' => number_format($f * $public_price * $value['area_build'], 2),
                        'rubbish_price' => number_format($f * $rubbish_price, 2),
                        'total_price' => number_format(number_format($f * $public_price * $value['area_build'], 2) + number_format($f * $price * $value['area_build'], 2) + number_format($f * $rubbish_price, 2), 2)
                    );
                }
            }
            for ($i = 1; $i <= $m; $i++) {
                foreach ($owner as $value) {
                    $f = 1;
                    $data[$value['id']][] = [
                        'check' => true,
                        'time' => $y . "年" . $i . '月',
                        'code' => $y . '-' . $i,
                        'price' => number_format($f * $price * $value['area_build'], 2),
                        'public_price' => number_format($f * $public_price * $value['area_build'], 2),
                        'rubbish_price' => number_format($f * $rubbish_price, 2),
                        'total_price' => number_format(number_format($f * $public_price * $value['area_build'], 2) + number_format($f * $price * $value['area_build'], 2) + number_format($f * $rubbish_price, 2), 2)
                    ];
                }
            }
        }

        $fee = TradePropertyModel::create()->where(['uid' => $uid, 'status' => 1])->all();
        if (!empty($fee)) {
            foreach ($data as $k => $v) {
                foreach ($v as $kk => $vv) {
                    foreach ($fee as $f) {
                        if ($vv['code'] == $f['code'] && $k == $f['hid']) {
                            unset($data[$k][$kk]);
                        }
                    }
                }
                $data[$k] = array_values($data[$k]);
            }
        }

        $ho = [];
        $totalPrice = [];
        foreach ($owner as $h) {
            $ho[] = [
                'id' => $h['id'],
                'name' => $h['project'] . $h['region'] . $h['build'] . '号楼' . $h['unit'] . '单元' . $h['floor'] . '0' . $h['house'] . '室'
            ];
            $totalPrice[$h['id']] = number_format(array_sum(array_column($data[$h['id']], 'total_price')), 2);
        }
        $result = [
            'type' => $type,
            'house' => $ho,
            'data' => $data,
            'total' => $totalPrice
        ];
        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $result
        ];
    }

    /**
     * @param $uid
     * @param $hid
     * @param $param
     * @param $ip
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function createOrder($uid, $hid, $param, $ip): array
    {
        $par = [];
        $order_no = DataConversion::getInstance()->get_order_no();
        $total_amount = 0;
        $first = $param[0]['code'];
        $end = '';
        foreach ($param as $item) {
            if ($item['check'] == true) {
                $end = $item['code'];
                $total_amount += $item['total_price'];
                $par[] = [
                    'uid' => $uid,
                    'hid' => $hid,
                    'order_no' => $order_no,
                    'code' => $item['code'],
                    'price' => $item['price'],
                    'public_price' => $item['public_price'],
                    'rubbish_price' => $item['rubbish_price'],
                    'total_price' => $item['total_price'],
                ];
            }
        }
        if (empty($par)) {
            return [
                'result' => false,
                'msg' => '订单未选'
            ];
        }
        if ($first != $end) {
            $memo = '【物业费】' . $first . '月 ~ ' . $end . '月物业费缴费' . $total_amount . '元';
        } else {
            $memo = '【物业费】' . $first . '月物业费缴费' . $total_amount . '元';
        }
        $res = TradePropertyModel::create()->saveAll($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '订单生成失败'
            ];
        }

        $oPar = [
            'uid' => $uid,
            'type' => 1,
            'order_no' => $order_no,
            'amount_real' => $total_amount,
            'status' => 2,
            'order_remark' => $memo
        ];

        $res = OrderModel::create()->addOrder($oPar);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '订单生成失败'
            ];
        }

        $appid = ConfigService::getInstance()->getSys("mxappid", 'wepay');
        $mxappid = ConfigService::getInstance()->getSys("mxminiappid", 'wepay');
        $mchid = ConfigService::getInstance()->getSys("mchid", 'wepay');
        $password = ConfigService::getInstance()->getSys("mxpassword", 'wepay');
        $cert = ConfigService::getInstance()->getSys("cert", 'wepay');
        $key = ConfigService::getInstance()->getSys("key", 'wepay');
        $notifyUrl = ConfigService::getInstance()->getSys("wenotify_url", 'wepay');
        $user = UserModel::create()->getUser(['id' => $uid]);
        if (!$user || $user->we_code == '') {
            return [
                'result' => false,
                'msg' => 'openidNotFound'
            ];
        }

        $res = Payment::getInstance()->mxWepay($appid, $mxappid, $mchid, $password, $cert, $key, $user->we_code, $order_no, $oPar['amount_real'] * 100, $memo, $notifyUrl, $ip);
        $result = $res->toArray();

        $resOrder = OrderModel::create()->update(['status' => 3], ['order_no' => $order_no]);
        if (!$resOrder) {
            return [
                'result' => false,
                'msg' => '系统错误，请稍后重试'
            ];
        }

        return [
            'result' => true,
            'msg' => '成功',
            'data' => $result ?? 'success'
        ];
    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getOrder($uid, $id): array
    {
        $order = OrderModel::create()->getOrder(['id' => $id, 'uid' => $uid]);
        if (!$order) {
            return [
                'result' => false,
                'msg' => '该订单不存在'
            ];
        }

        if ((time() - $order->create_time > 3600) && $order->status == 2) {
            return [
                'result' => false,
                'msg' => '该订单已失效'
            ];
        }

        $data = [
            'id' => $order->id,
            'order_no' => $order->order_no,
            'amount_real' => $order->amount_real,
            'status' => $order->status,
            'order_remark' => $order->order_remark,
            'status_label' => ConfigService::getInstance()->getOrderStatus($order->status),
            'time' => date("Y-m-d h:i:s", $order->create_time)
        ];

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     */
    public function getMyOrders($uid, $type): array
    {
        $where = ['is_deleted' => 0, 'uid' => $uid];
        if ($type == 99) {
            $orders = OrderModel::create()->where("type", [1, 2], "IN")->where($where)->where("status", 4, ">=")->order("id", 'DESC')->all();
        } else {
            $orders = OrderModel::create()->where($where)->where(['type' => $type])->where("status", 4, ">=")->order("id", 'DESC')->all();
        }

        if (empty($orders)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($orders as $value) {
            $data[] = [
                'id' => $value['id'],
                'icon' => $value['type'] == 1 ? 'https://s4.ax1x.com/2022/02/22/HzJN26.png' : 'https://s4.ax1x.com/2022/02/22/HzJUxK.png',
                'type' => $value['type'],
                'title' => $value['type'] == 1 ? '物业费' : '车位费',
                'order_no' => $value['order_no'],
                'amount_real' => $value['amount_real'],
                'status' => $value['status'],
                'status_label' => ConfigService::getInstance()->getOrderStatus($value['status']),
                'time' => date("Y-m-d H:i:s", $value['create_time']),
                'order_remark' => $value['order_remark']
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    public function getMyFee($uid)
    {

    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delVisit($uid, $id): array
    {
        $res = PropertyVisitModel::create()->update(['is_deleted' => 1], ['id' => $id, 'uid' => $uid]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];

    }

    /**
     * @param $uid
     * @param $id
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delRepair($uid, $id): array
    {
        $res = PropertyRepairModel::create()->update(['is_deleted' => 1], ['id' => $id, 'uid' => $uid]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => "操作失败"
            ];
        }

        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $uid
     * @param $sid
     * @param $star
     * @param $content
     * @param $is_anonymous
     * @return array
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception|Throwable
     */
    public function keeperComment($uid, $sid, $star, $content, $is_anonymous): array
    {
        $time = strtotime(date("Y-m-01"));

        $comment = KeeperCommentModel::create()->where("create_time", $time, ">=")->getDynamic(['uid' => $uid, 'sid' => $sid]);
        if ($comment) {
            return [
                'result' => false,
                'msg' => '当月已评论，请下月再来'
            ];
        }
        $par = [
            'uid' => $uid,
            'sid' => $sid,
            'star' => $star,
            'content' => $content,
            'is_anonymous' => $is_anonymous
        ];

        $res = KeeperCommentModel::create()->addDynamic($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }

        $star = KeeperCommentModel::create()->where(['sid' => $sid])->sum("star");
        $num = KeeperCommentModel::create()->where(['sid' => $sid])->count("id");

        $s = round(($star / $num));
        $r = ConfigHouseKeeperModel::create()->where(['id' => $sid])->update(['star' => $s]);

        return [
            'result' => true,
            'msg' => '评论成功',
            'data' => 'success'
        ];
    }
}